package com.liwen.service.file;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: li.rui
 * @time: 2018-04-18 16:38
 * @description:
 */
public class Storage<T> {
    private IProducer<T> producer;
    private Lock lock;
    private volatile boolean flag = true;

    public Storage(IProducer<T> producer) {
        this.producer = producer;
        this.lock = new ReentrantLock();
    }

    public T getProduct() {
        T products = null;
        lock.lock();
        try {
            if (flag) {
                products = producer.produce();
                if (null == products)
                    flag = false;
            }
        } catch (Exception e) {
        } finally {
            lock.unlock();
        }
        return products;
    }
}
